package gorm

import (
	"go-main/library/log"
	"gorm.io/gorm"
)

/*
func (db *DB) nodeproc(e <-chan struct{}) {
	var err error
	if db.dis == nil {
		return
	}
	for {
		<-e
		nodes := db.nodeList()
		if len(nodes) == 0 {
			continue
		}
		oldOrmConnections := db.Db
		var dsns []string

		// 建立新连接
		for _, node := range nodes {
			dsns = append(dsns, genDSN(db.conf.DSN, node))
		}
		if len(dsns) == 0 {
			log.Error("tidb: no nodes ignore event")
			continue
		}
		db.Db, err = connect(db.conf, dsns)
		if err != nil {
			log.Error("orm: some err in new connection: %v, ignored", err)
			return
		}
		log.Infof("tidb: new nodes: %v", nodes)

		// 关闭旧链接
		go db.closeConns(oldOrmConnections)
	}
}
*/

func (db *DB) closeConns(orm *gorm.DB) {
	sqlDb, err := orm.DB()
	if err != nil {
		log.Errorf("orm: some err in close connection: %v, ignored", err)
		return
	}
	sqlDb.Close()
}
